
(function () { 
	
	Lhs.ui.CreateCommunicate_TelevisionWindow = function (arg) {
	var pathimage=Ti.App.Properties.getString('pathimage');
    var win = Titanium.UI.createWindow({
        id: 'Communicate_Television',
        title: 'TRUYỀN HÌNH ANALOG',
        fullscreen: true,
        backgroundImage:pathimage+'bg.png',
        navBarHidden:true,
    });
    
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_'+win.id, function () {
    	Lhs.navGroup.close(win, {
             animated: true
        });
    	
	});
	
	//HEADER ON WINDOWS
    var main_header=require('controls/header').Createheader({
		  	idWinParent:win.id,
		   	height:Ti.App.Properties.getInt('height_Header'),//44,
		  	title:'TRUYỀN HÌNH ANALOG',
    })
	win.add(main_header);
	
	//LABEL
	/*
	var Lable_Company = Ti.UI.createLabel({
        text:arg.name,
        font:{
            fontSize:'24dp',
        	fontWeight:'bold'
        },
        color:'#724b37',
        top:main_header.height,
        height:'50',
        
    });
	win.add(Lable_Company);
	*/
	
	
	//Search bar
	/*
	var search = Titanium.UI.createSearchBar({
	    barColor:'#000', 
	    showCancel:true,
	    top:main_header.height,
	    height:50,
	});
	win.add(search);
	*/
	
	
	
    //TABLE VIEW
   
    var table_CategoryDetail=Ti.UI.createTableView({
    		
    		separatorColor:'Transparent',
		  	backgroundColor:'Transparent',
		  	top:main_header.height+main_header.top,
		  
    })
    table_CategoryDetail.addEventListener('click', function (e) {
    	Lhs.navGroup.open(Lhs.ui.CreateCompanyDetailWindow({
			cid:e.rowData.id,
		}), 
		{
		    animated: true,
		    navBarHidden:true
		});	
	});
	win.add(table_CategoryDetail);
    //GET DATA JSON FROM INTERNET======>host www.e-media.vn
    
    
    ///////////////////LOADING WINDOWS
    var LoadListData = function(e) {
    	var datajson=e.datajson;
 		json = JSON.parse(datajson);
		return json;
    }
    var LoadListDataLocal = function(e) {
 		var json=require('db').selectCategoryDetail(arg.id);
		return json;
    }
    var CreateTableViewRow = function(e) {
    	var item=e.data;
        var row = Ti.UI.createTableViewRow({
	       
            height:Ti.App.Properties.getInt('height_TableviewRow'),//90,
			id:item.id,
			name:item.title
		})
		var Image_Left = Ti.UI.createImageView({
			image:item.logo,
			left:0,
			width:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
			height:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
		})
		
		
		
		var nameLabel = Ti.UI.createLabel({
            text:item.title,
            font:{
                fontSize:Ti.App.Properties.getInt('fontsize_TableviewRow'),//'17dp',
            	fontWeight:'bold'
            },
            left:Ti.App.Properties.getInt('height_TableviewRow')+4,//80,
            color:'#724b37',
        });
        var vi_line = Titanium.UI.createView({
			bottom:0,
			width:'100%',
			height:2,
			backgroundImage: pathimage+'img_devide_line_setting.png',
		 	touchEnabled: false,
		}); 
		
        if (Ti.Network.online) 
 		{
 			//Insert data when connect
        	require('db').insertCategoryDetail(item,arg.id)
 		}
		row.add(nameLabel);
		row.add(Image_Left);
		row.add(vi_line)
		return row;
    }
    var LoadList = function(e) {
     	/*
		var datajson=e.datajson;
		//Ti.API.info (datajson)
 		var tableData=[];
 		//Convert Text to Json Array
 		json = JSON.parse(datajson);
 		//Ti.API.info (json.length)
 		//alert(json.length)
 		
 		*/
 		var tableData=[];
 		var json=[];
 		
 		if (Ti.Network.online) 
 		{
 			json=LoadListData(e);
 		}
 		else
 		{
 			json=LoadListDataLocal(e);
 		}
 		//Ti.API.info (json)
 		/*
	    //Import data Json to Tableview
	    if(json.length==0)
	    {
	    	require('settings').Alert_error(win,Globals.connect_pop);
	    }
	    for (i = 0; i < json.length; i++) {
	        var item = json[i];
			Ti.API.info (item)
	        var row = Ti.UI.createTableViewRow({
		       
	            height:Ti.App.Properties.getInt('height_TableviewRow'),//90,
				id:item.id,
				name:item.title
			})
			var Image_Left = Ti.UI.createImageView({
				image:item.logo,
				left:0,
				width:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
				height:Ti.App.Properties.getInt('height_TableviewRow')-2,//79,
			})
			
			
			
			var nameLabel = Ti.UI.createLabel({
	            text:item.title,
	            font:{
	                fontSize:Ti.App.Properties.getInt('fontsize_TableviewRow'),//'17dp',
	            	fontWeight:'bold'
	            },
	            left:Ti.App.Properties.getInt('height_TableviewRow')+4,//80,
	            color:'#724b37',
	        });
	        var vi_line = Titanium.UI.createView({
				bottom:0,
				width:'100%',
				height:2,
				backgroundImage: pathimage+'img_devide_line_setting.png',
			 	touchEnabled: false,
			}); 
			
	        if(e.isconnect==1)
	 		{
	 			//Insert data when connect
	        	require('db').insertCategoryDetail(item,arg.id)
	 		}
			row.add(nameLabel);
			row.add(Image_Left);
			row.add(vi_line)
	        //tableData.push(row);
			table_CategoryDetail.appendRow(row)
        }
 		//table_CategoryDetail.setData(tableData);
 		//Lhs.ui.activityIndicator.hideModal();
 		*/
 		//Ti.API.info ('Chieu dai c '+json.length)
 		if (json.length==0) {
 			require('settings').Alert_error(win,Globals.connect_pop);
 		}else
 		require('settings').LoadingData2TableView(table_CategoryDetail,json,CreateTableViewRow,15);
	};
	
	var LoadList_error = function(e) {
		
		
		if(Ti.Platform.osname == 'android')
		{
			require('settings').createAlertDialog(Globals.title,Globals.connecterror);
			//Lhs.ui.activityIndicator.hideModal();
		}
		else
	   		require('settings').Alert_error(win);
	};
	 
	
	
    
    win.addEventListener('open', function (e) {
    	
    	 if (Ti.Network.online) {
			//Load data when online
			var jsonurl='http://www.e-media.vn/emedia.php?tv_cab';
			var param='';
			//Lhs.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			require('settings').GetJsonFunction_GET(jsonurl,param,LoadList,LoadList_error);
			//Lhs.ui.activityIndicator.hideModal();
		 }
		 else
		 {
		 	//error in connect with internet
		 	//require('settings').createAlertConnectError();
		 
		 	//var ee=[{isconnect:0}];
		 	LoadList();
		 	//LoadList_error()
		 }
		
	});
 	return win;
       
    };
})();